<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteTypeById()" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input name="name" id="myName">
									<button class="btn btn-default" onclick="getTemplateList()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody"></tbody>
			                  </table>

							<!--分页-->
							<div id="Number"></div>
							<input type="hidden"  id="page" value="1" size="1px" >
							<input type="hidden"  id="pages" size="1px" >
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板增加</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input id="add_name" class="form-control" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brand" name="specIds" style="width: 100%" multiple>
							<option value="-1">请选择</option>
							<option th:each="brand : ${list}" th:value="${brand.id}">[[${brand.name}]]</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="spec" name="brandIds" style="width: 100%" multiple>
							<option value="-1">请选择</option>
							<option th:each="spec : ${spList}" th:value="${spec.id}">[[${spec.specName}]]</option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" name="kuozan" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" name="kuozan" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 编辑窗口 -->
  <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabe">商品类型模板修改</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <input id="id_update" type="hidden" class="form-control" placeholder="商品类型">
					  <tr>
						  <td>商品类型</td>
						  <td><input id="name_update" class="form-control" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brand_update" name="brandIds" style="width: 100%" multiple>
								  <option value="-1">请选择</option>
								  <option th:each="brand : ${list}" th:value="${brand.id}">[[${brand.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="spec_update" name="specIds" style="width: 100%" multiple>
								  <option value="-1">请选择</option>
								  <option th:each="spec : ${spList}" th:value="${spec.id}">[[${spec.specName}]]</option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="tbodys"></tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>

<script>
	$(function (){
		$("#brand").select2()
		$("#spec").select2()
		$("#brand_update").select2()
		$("#spec_update").select2()
		getTemplateList();
	})

	//查询
	function getTemplateList(){
		var page = parseInt($("#page").val());
		$.ajax({
			url : "/type/templatePage",
			data : {pageNumber : page,name:$("#myName").val()},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				var data = result.data.rows;
				var tr = '';
				for (var i=0;i<data.length;i++){
					var specIds = JSON.parse(data[i].specIds);
					var brandIds = JSON.parse(data[i].brandIds);
					var customAttributeItems = JSON.parse(data[i].customAttributeItems);
					tr += '<tr>'
					tr += '<td><input type="checkbox" value="'+data[i].id+'" id="box"></td>';
					tr += '<td>'+data[i].id+'</td>';
					tr += '<td>'+data[i].name+'</td>';
					tr+='<td>'
					for(var a = 0;a<specIds.length;a++){
						tr += specIds[a].text + ",";
					}
					tr+='</td>';
					tr+='<td>';
					for(var b = 0;b<brandIds.length;b++){
						tr += brandIds[b].text + ",";
					}
					tr+='</td>';
					tr+='<td>';
					for(var c = 0;c<customAttributeItems.length;c++){
						tr += customAttributeItems[c].text + ',';
					}
					tr+='</td>';
					tr += '<td class="text-center">';
					tr += '<button type="button" class="btn bg-olive btn-xs" onclick="updateById('+data[i].id+')" data-toggle="modal" data-target="#UpdateModal">修改';
					tr += '</button>';
					tr += '<button type="button" class="btn bg-olive btn-xs" onclick="deleteById('+data[i].id+')" data-toggle="modal" data-target="#UpdateModal">删除';
					tr += '</button>';
					tr += '</td>';
					tr += '</tr>';
				}

				$("#tbody").html(tr);

				//分页
				var num = ''
				num += '<button onclick="toPage(3)">首页</button>';
				num += '<button onclick="toPage(1)">上一页</button>';
				num += '<button onclick="toPage(2)">下一页</button>';
				num += '<button onclick="toPage(4)">末页</button>';
				$("#Number").html(num);
				var d = result.data;
				$("#pages").val(d.pages);
				$("#total").val(d.total);
				$("#pageSize").val(d.pageSize);

			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//分页
	function toPage(sb){

		var page = parseInt($("#page").val());
		var pages = parseInt($("#pages").val());

		if(sb == 1){
			$("#page").val(page - 1);
		}else if(sb == 2){
			if (page!=pages){
				$("#page").val(page + 1);
			}
		}else if(sb == 3){
			$("#page").val(1);
		}else if(sb == 4){
			$("#page").val(pages);
		}
		getTemplateList()
	}
	//增加
	function add(){
		//获取品牌信息
		var brand = [];
		var brandList = $("#brand").select2('data');
		for(var i=0;i<brandList.length;i++){
			var id = brandList[i].id;
			var text = brandList[i].text;
			var brandJSON = {id:id,text:text};
			brand.push(brandJSON)
		}
		var brandLis = JSON.stringify(brand);

		//获取规格信息
		var spec = [];
		var specList = $("#spec").select2('data');
		for(var i=0;i<specList.length;i++){
			var id = specList[i].id;
			var text = specList[i].text;
			var specJSON = {id:id,text:text};
			spec.push(specJSON)
		}
		var specLis = JSON.stringify(spec)

		//获取扩展属性的信息
		var kuozanList = [];
		$("[name=kuozan]").each(function (i,e){
			var kuozan = {text : $(e).val()};
			kuozanList.push(kuozan);
		})
		var kuozanJson = JSON.stringify(kuozanList);
		$.ajax({
			url : "/type/addInfo",
			data : {name:$("#add_name").val(),specIds:specLis,brandIds:brandLis,customAttributeItems:kuozanJson},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				if(result.code == 10000){
					alert("增加成功");
					location.reload();
				}else{
					alert("增加失败")
				}

			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//回显
	function updateById(id){
		$.ajax({
			url : "/type/getTypeById",
			data : {id:id},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				var data = result.data;
				//给id和商品name赋值
				$("#id_update").val(data.id);
				$("#name_update").val(data.name);

				//品牌回显
				var brandList = JSON.parse(data.brandIds);
				var  brands= [];
				for (var i=0;i<brandList.length;i++){
					brands.push(brandList[i].id);
				}
				$("#brand_update").val(brands).trigger("change");

				//规格回显
				var specLis = JSON.parse(data.specIds);
				var specs = [];
				for (var i=0;i<specLis.length;i++){
					specs.push(specLis[i].id);
				}
				$("#spec_update").val(specs).trigger("change");

				//扩展属性回显
				var kuozanList = JSON.parse(data.customAttributeItems);
				var tr = '';
				for (var i=0;i<kuozanList.length;i++){
					tr+='<tr>';
					tr+='<td><input type="checkbox" class="icheckbox_square-blue"></td>';
					tr+='<td><input class="form-control" value="'+kuozanList[i].text+'" name="kuozan2" placeholder="属性名称"></td>';
					tr+='<td>';
					tr+='<button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除';
					tr+='</button>';
					tr+='</td>';
					tr+='</tr>';

				}
				$("#tbodys").html(tr);
			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//修改
	function updateInfo(){
		//获取品牌信息
		var brand = [];
		var brandList = $("#brand_update").select2('data');
		for(var i=0;i<brandList.length;i++){
			var id = brandList[i].id;
			var text = brandList[i].text;
			var brandJSON = {id:id,text:text};
			brand.push(brandJSON)
		}
		var brandLis = JSON.stringify(brand);

		//获取规格信息
		var spec = [];
		var specList = $("#spec_update").select2('data');
		for(var i=0;i<specList.length;i++){
			var id = specList[i].id;
			var text = specList[i].text;
			var specJSON = {id:id,text:text};
			spec.push(specJSON)
		}
		var specLis = JSON.stringify(spec)

		//获取扩展属性的信息
		var kuozanList = [];
		$("[name=kuozan2]").each(function (i,e){
			var kuozan = {text : $(e).val()};
			kuozanList.push(kuozan);
		})

		var kuozanJson = JSON.stringify(kuozanList);
		$.ajax({
			url : "/type/updateInfo",
			data : {id:$("#id_update").val(),name:$("#name_update").val(),specIds:specLis,brandIds:brandLis,customAttributeItems:kuozanJson},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				if(result.code == 10000){
					alert("修改成功");
					location.reload();
				}else{
					alert("修改失败")
				}

			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//批量删除
	function deleteTypeById(){
		var ids = $("#box:checked")
		var typeIds = [];
		for (var i = 0; i < ids.length; i++) {
			typeIds.push(ids[i].value)
		}
		$.ajax({
			url: "/type/deleteTypeById",
			data: {id: typeIds},
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
					window.location.reload();
					alert(result.message);
				} else {
					alert(result.message);

				}
			},
			error: function (result) {
				alert("系统异常")
			}
		})
	}
	//删除
	function deleteById(id){
		$.ajax({
			url: "/type/deleteById",
			data: {id: id},
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
					window.location.reload();
					alert(result.message);
				} else {
					alert(result.message);

				}
			},
			error: function (result) {
				alert("系统异常")
			}
		})
	}
</script>